package com.itht.baoleme.controller;

import com.itht.baoleme.domain.AccountDTO;
import com.itht.baoleme.domain.web.*;
import com.itht.baoleme.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
public class TestController {

    @Autowired
    private AccountService accountService;

    @PostMapping(value = "/insert")
    public void insert(@RequestParam(value = "userName") String userName,
                       @RequestParam(value = "password") String password) {
        accountService.insert(userName, password);
    }


    // GetMapping 等价于 @RequestMapping(value = "/test", method = RequestMethod.GET)
    @GetMapping( "/test")
    public String test() {
        return "success";
    }



    @GetMapping(value = "/testNoData")
    public WebServiceMessage<Void> testNoData() {
        // 新增用户操作
        // 成功之后返回成功信息给客户端
        return WebServiceMessage.success();
    }

    @GetMapping(value = "/testError")
    public WebServiceMessage testError() {
        // 业务逻辑 进行操作，发现用户不存在
        return WebServiceMessage.error();
    }

    // 确认收获 需要订单的状态是发货中待收货
    @GetMapping(value = "/confirm/{orderId}")
    public WebServiceMessage confirmOrder(@PathVariable("orderId") Long orderId) {
        // orderId参数校验
        if (null == orderId || orderId < 0L) {
            return WebServiceMessage.error(WebCodeEnum.PARAM_ERROR);
        }
        // 根据订单id从数据库中找到订单
        OrderDTO orderDTO = findOrderById(orderId);
        if (null == orderDTO) {
            return WebServiceMessage.error(WebCodeEnum.ORDER_NOT_EXIST);
        }

        if (!Objects.equals(2, orderDTO.getStatus())) {
            // 订单状态不为2的不能确认，订单状态异常
            return WebServiceMessage.error("订单状态异常");
        }
        // 到了这里说明这个订单是可以确认收货的
        // update order
        orderDTO.setStatus(3);
        //TODO dao.update();
        return WebServiceMessage.success();
    }

    /**
     * 评价订单
     * @param orderId
     * @return
     */
    @GetMapping(value = "/comment/{orderId}")
    public WebServiceMessage commentOrder(@PathVariable("orderId") Long orderId) {
        // orderId参数校验
        if (null == orderId || orderId < 0L) {
            return WebServiceMessage.error(WebCodeEnum.PARAM_ERROR);
        }
        // 根据订单id从数据库中找到订单
        OrderDTO orderDTO = findOrderById(orderId);
        if (null == orderDTO) {
            return WebServiceMessage.error(WebCodeEnum.ORDER_NOT_EXIST);
        }
        // StatusEnum(3, "已经确认收获，待评价")
        if (!Objects.equals(StatusEnum.WAIT_COMMENT.getStatus(), orderDTO.getStatus())) {
            // 订单状态不为3的不能评价，订单状态异常
            return WebServiceMessage.error("订单状态异常");
        }

        // 到了这里说明这个订单是可以评价的
        // update order
        orderDTO.setStatus(OrderDTO.OrderStatusEnum.FINISHED.getStatus());
        // TODO dao.update();
        return WebServiceMessage.success();
    }

    public OrderDTO findOrderById(Long orderId) {
        // dao find order
        return new OrderDTO();
    }

    /**
     * 根据用户id获取用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping(value = "/user/info/{id}")
    public AccountDTO getUserInfoById(@PathVariable Long id) {
        return accountService.getUserInfoById(id);
    }

    @GetMapping(value = "/user/info/v2/{id}")
    public WebServiceMessage<AccountDTO> getUserInfoByIdV2(@PathVariable Long id) {
        AccountDTO accountDTO = accountService.getUserInfoById(id);
        return WebServiceMessage.success(accountDTO);
    }

    @GetMapping(value = "/user/info/v3/{id}")
    public WebServiceMessage<AccountDTO> getUserInfoByIdV3(@PathVariable Long id) {
        return accountService.getUserInfoByIdV2(id);
    }

    @GetMapping(value = "/user/list")
    public List<AccountDTO> getUserList() {
        return null;
    }


    @GetMapping(value = "/getUser/{userId}")
    public WebServiceMessage getUser(@PathVariable("userId") Long userId) throws WebServiceException {
        return WebServiceMessage.success(accountService.getUser(userId));
    }
}
